package com.tfswx.jkgl.core.utils;

import com.tfswx.jkgl.core.exception.AppException;
import org.apache.commons.lang3.StringUtils;

/**
 * @author Ricky
 * @date 2021/1/29
 */
public class ExceptionUtil {
    public static String getMessage(Throwable e) {
        if (e.getCause() instanceof AppException) {
            e = e.getCause();
        }
        String message = e.getMessage();
        if (message == null) {
            message = "";
        }
        StringBuilder errmsgSb = new StringBuilder(message);
        Throwable cause = e;
        while ((cause = cause.getCause()) != null) {
            String inMsg = cause.getMessage();
            if (StringUtils.isNotBlank(inMsg)) {
                if (StringUtils.isNotBlank(errmsgSb.toString())) {
                    errmsgSb.append("-->");
                }
                errmsgSb.append(inMsg);
            }
        }
        String errMsg = errmsgSb.toString();
        if (errMsg.startsWith("Required request body is missing:")) {
            errMsg = "缺少必需的请求Body";
        }
        return errMsg;
    }
}
